let lbbService = require('../../../pages/service/service.js')
let latitude = ''
let longitude = ''
let addressDetail = ''
let taskTypely=''
async function operateMethod(params, taskId, hospitalId, taskType, operationTypeCode) {
    taskTypely=taskType
    //改变操作状态
    params.forEach(item => {
        if (item.operationTypeCode == operationTypeCode) {
            item.handleStatus = 1
        }
    })
    console.log(252525, params, taskId)
    //查找没有提交操作的任务
    if (params.find(item => item.handleStatus == 0)) {
        params.forEach(item => {
            if (!item.handleStatus) {
                if(taskTypely=='long'){
                    counterPageLong(item.operationTypeCode, taskId, params, hospitalId, taskType)
                }else{
                    counterPage(item.operationTypeCode, taskId, params, hospitalId, taskType)
                }    
            }
        })
    } else {
        await getWXLocation(taskId)
    }
}
//获取当前经纬度
function getWXLocation(taskId) {
    wx.showLoading({
        title: '定位中...',
        mask: true,
    })
    return new Promise((resolve, reject) => {
        let _locationChangeFn = (res) => {
            resolve(res);
            latitude = res.latitude,
                longitude = res.longitude
            getAddress(taskId)
            wx.hideLoading()
            wx.offLocationChangeError(_locationChangeFn)
            wx.offLocationChange(_locationChangeFn)
            wx.stopLocationUpdate()
        }
        wx.startLocationUpdate({
            success: (res) => {
                wx.onLocationChangeError(_locationChangeFn)
                wx.onLocationChange(_locationChangeFn)
            },
            fail: (err) => {
                wx.hideLoading()
                reject()
                getAddress(taskId)
            }
        })
    })
}
//根据经纬度获取详细地址
function getAddress(taskId) {
    wx.request({
        url: 'https://api.map.baidu.com/geocoder/v2/',
        method: 'GET',
        data: {
            location: latitude + ',' + longitude,
            output: 'json',
            coordtype: 'wgs84ll',
            pois: 1,
            s: 1,
            latest_admin: 1,
            ak: 'GQKwKHdtu8CgmxXIkXdyTkdtlfvML4Gg'
        },
        success(res) {
            console.log('>>', res)
            if (res.data.status == 0 && res.data.result.formatted_address) {
                let address = res.data.result.formatted_address
                addressDetail = address
                if (taskTypely=='long') {
                    lbbService.TaskItemsOk({
                        taskLongStateId: taskId,
                        handleLocation: addressDetail
                    }, function (res) {
                        wx.setStorageSync('markLongId', taskId)
                        wx.navigateBack({
                            delta: 1
                        })
                    })
                } else {
                    lbbService.HandleOk({
                        taskDevOpsId: taskId,
                        handleDesc: '',
                        handleLocation: addressDetail
                    }, function (res) {
                        wx.setStorageSync('markShortId', taskId)
                        wx.navigateBack({
                            delta: 1
                        })
                    })
                }
            } else {
                wx.showToast({
                    title: '获取位置失败,请打开手机定位重新签到',
                    icon: 'none',
                    duration: 3000
                })
            }
        },
        fail() {
            addressDetail = null
        }
    })
}
//市场临时任务去未操作提交页面的判断
function counterPage(code, taskId, configOperationlist, hospitalId, taskType) {
    configOperationlist = JSON.stringify(configOperationlist)
    if (code == 'B19A03A01A01') {
        wx.redirectTo({
            url: `/pkgTask/pages/prebid-communication/index?taskId=${taskId}&type=${'pre_bid_communication'}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A01`
        })
    } else if (code == 'B19A03A01A02') {
        wx.redirectTo({
            url: `/pkgTask/pages/Internal-bidding/index?taskId=${taskId}&type=${'in_invite_bid'}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A02`
        })
    } else if (code == 'B19A03A01A03') {
        wx.redirectTo({
            url: `/pkgTask/pages/Internal-bidding/index?taskId=${taskId}&type=${'assist_invite_bid'}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A03`
        })
    } else if (code == 'B19A03A01A04') {
        wx.redirectTo({
            url: `/pkgTask/pages/hospital-Information/index?taskId=${taskId}&hospitalId=${hospitalId}&taskType=${taskType}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A04`
        })
    } else if (code == 'B19A03A01A05') {
        wx.redirectTo({
            url: `/pkgTask/pages/agent-link/index?taskId=${taskId}&type=${'agent_communication'}&hospitalId=${hospitalId}&taskType=${taskType}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A05`
        })
    } else if (code == 'B19A03A01A06') {
        wx.redirectTo({
            url: `/pkgTask/pages/prebid-communication/index?taskId=${taskId}&type=${'hospital_communication'}&hospitalId=${hospitalId}&taskType=${taskType}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A06`
        })
    } else if (code == 'B19A03A01A07') {
        wx.redirectTo({
            url: `/pkgTask/pages/look-site/index?taskId=${taskId}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A07`
        })
    } else if (code == 'B19A03A01A08') {
        wx.redirectTo({
            url: `/pkgTask/pages/usage-report/index?taskId=${taskId}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A08`
        })
    } else if (code == 'B19A03A01A09') {
        wx.redirectTo({
            url: `/pkgTask/pages/agent-information/index?taskId=${taskId}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A09`
        })
    } else if (code == 'B19A03A01A10') {
        wx.redirectTo({
            url: `/pkgTask/pages/agent-link/index?taskId=${taskId}&type=${'handle_contract'}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A10`
        })
    } else if (code == 'B19A03A01A11') {
        wx.redirectTo({
            url: `/pkgTask/pages/agent-link/index?taskId=${taskId}&type=${'agent_urgent_communication'}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A11`
        })
    } else if (code == 'B19A03A01A12') {
        wx.redirectTo({
            url: `/pkgTask/pages/prebid-communication/index?taskId=${taskId}&type=${'hospital_urgent_communication'}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A12`
        })
    }
}
//市场长期任务去未操作提交页面的判断
function counterPageLong(code, taskId, configOperationlist, hospitalId, taskType) {
    let that=this
    let activeKey=''
    configOperationlist = JSON.stringify(configOperationlist)
    if (code == 'B19A04A01A01') {
        wx.redirectTo({
            url: `/pkgTask/pages/prebid-communication/index?taskId=${taskId}&type=${'pre_bid_communication'}&hospitalId=${hospitalId}&operationlist=${configOperationlist}&taskType=${taskType}&activeKey=${activeKey}&operationTypeCode=B19A04A01A01`
        })
    } else if (code == 'B19A04A01A05') {
        wx.redirectTo({
            url: `/pkgTask/pages/maintenance-status/index?taskId=${taskId}&type=${'agent_relationship'}&hospitalId=${hospitalId}&taskType=${taskType}&activeKey=${activeKey}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A05`
        })
    } else if (code == 'B19A04A01A03') {
        wx.redirectTo({
            url: `/pkgTask/pages/hospital-Information/index?taskId=${taskId}&hospitalId=${hospitalId}&taskType=${taskType}&activeKey=${activeKey}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A03`
        })
    } else if (code == 'B19A04A01A02') {
        wx.redirectTo({
            url: `/pkgTask/pages/agent-link/index?taskId=${taskId}&type=${'agent_communication'}&hospitalId=${hospitalId}&taskType=${taskType}&operationlist=${configOperationlist}&activeKey=${activeKey}&operationTypeCode=B19A04A01A02`
        })
    } else if (code == 'B19A04A01A06') {
        wx.redirectTo({
                url: `/pkgTask/pages/maintenance-status/index?taskId=${taskId}&type=${'hospital_relationship'}&hospitalId=${hospitalId}&taskType=${taskType}&activeKey=${activeKey}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A06`
        })
    } else if (code == 'B19A04A01A07') {
        wx.redirectTo({
            url: `/pkgTask/pages/speaker-communication/index?taskId=${taskId}&hospitalId=${hospitalId}&activeKey=${activeKey}&taskType=${taskType}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A07`
        })
    } 
}
module.exports = operateMethod